package java_interface;

import java.util.Objects;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/9/18 20:06
 * 当Student类实现Comparable接口，就表示当前类具备了可比较的能力
 * 根据compareTo的返回值，jvm就知道Student对象谁大谁小了
 */
public class Student implements Comparable{
    private String name;
    private int score;

    public Student(String name, int age) {
        this.name = name;
        this.score = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public int compareTo(Object o) {

            Student student = (Student) o;
            if(this.score>student.score){
                return 1;
            }else if(this.score<student.score){
                return -1;
            }else{
                return 0;
            }
    }
    public void showStu(){
        System.out.println(this.name+":"+this.score);
    }
    //重写Object类中的toString方法，println方法自动调用toString方法
    @Override
    public String toString(){
        return "name = "+this.name+",score = "+this.score;
    }
//快捷生成toString方法，alt+Insert
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", score=" + score +
//                '}';
//    }

    //直接在子类中写父类方法名称！
    public boolean equals(Object o){
        //1.判断obj和当前对象的关系
        if(this  == o){
            //这两个引用是同一个对象
            return true;
        }
        //2.脱掉obj的外衣，看他是不是Student类型，不是一个类型没有可比性
        if(!(o instanceof Student)){
            return false;
        }
        //3.obj确实是Student类型对象，且和this不是同一个对象
        //向下转型还原为Student类型
        Student student = (Student) o;
        return this.name.equals(student.name) && this.score == student.score;
    }
}
